<?php
/*****************************************************************************
**	Класс:	Общий класс (набор функций)										**
**	Автор:	Dark Dayver														**
**	Версия:	1.0b															**
**	Дата:	25/12-2009														**
*****************************************************************************/
if (IN_P_ENIGMA)
{
	die('HACK!');
}

class language
{
	var $main = array(); // Основной языковый массив
	var $menu = array(); // Языковый массив меню
	var $faction = array(); // Языковый массив фракций
	private $month = array(); // Масив локальных названий месяцев
	private $lang_dir; // Папка, где расположены все файлы языка

	function language($site_dir, $user_lang, $load_lang = false)
	{
		$lang = array();
		$this->lang_dir = $site_dir . 'language/' . $user_lang . '/';
		include($this->lang_dir . 'main.php');
		$this->load_language($lang);
		$lang = array();
		if ($load_lang && $load_lang != 'main')
		{
			if (file_exists($this->lang_dir . $load_lang . '.php'))
			{
				include($this->lang_dir . $load_lang . '.php');
				$this->load_language($lang);
				$lang = array();
			}
		}
	}

	private function load_language($lang)
	{
		foreach($lang as $key=>$value)
		{
			if (is_array($value))
			{
				$this->$key = $value;
			}
			else
			{
				$this->main[$key] = $value;
			}
		}
	}

	function local_menu()
	{
		global $work;

		$menu_array = $work->menu();
		for ($i = 1; $i <= count($menu_array); $i++)
		{
			if (isset($this->menu[$menu_array[$i]['L_URL']]) && !empty($this->menu[$menu_array[$i]['L_URL']]))
			{
				$menu_array[$i]['L_URL'] = $this->menu[$menu_array[$i]['L_URL']];
			}
		}
		return $menu_array;
	}

	function local_faction($faction)
	{
		if (isset($this->faction[$faction]) && !empty($this->faction[$faction]))
		{
			return $this->faction[$faction];
		}
		else
		{
			return $faction;
		}
	}

	function local_mail($to_email, $to_name, $template, $array_data)
	{
		global $work;

		$message = file_get_contents($this->lang_dir . 'mail/' . $template . '.eml');
		if (!$message) die ('Error template mail-file!');
		while (list($key, $val) = each($array_data))
		{
			$message = str_replace('{' . $key . '}', $val, $message);
		}
		$message = str_replace(chr(13) . chr(10), '{BR}', $message);
		$message = str_replace(chr(13), '{BR}', $message);
		$message = str_replace(chr(10), '{BR}', $message);
		// $message = $work->utf8_wordwrap($message);
		$message = str_replace('{BR}', chr(13) . chr(10), $message);
		$headers  = 'MIME-Version: 1.0' . PHP_EOL;
		$headers .= 'Content-type: text/plain; charset=UTF-8' . PHP_EOL;
		$headers .= 'Precedence: bulk' . PHP_EOL;
		$headers .= 'Return-path: <' . $work->config['admin_email'] . '>' . PHP_EOL;
		$headers .= 'Reply-To: ' . $work->config['admin_name'] . ' <' . $work->config['admin_email'] . '>' . PHP_EOL;
		$headers .= 'X-Mailer: Admin of ' . $work->config['title'] . PHP_EOL;
		$headers .= 'Message-ID: <' . time() . '.' . $work->config['admin_email'] . '>' . PHP_EOL;
		$headers .= 'From: ' . $work->config['admin_name'] . ' <' . $work->config['admin_email'] . '>' . PHP_EOL;
		@mail($to_name . ' <' . $to_email . '>', '=?UTF-8?B?' . base64_encode($this->email[$template]) . '?=', $message, $headers, '-f' . $work->config['admin_email']);
	}

	function local_time($format, $time)
	{
		$time = strtolower(date($format, (strtotime($time))));
		foreach ($this->month as $key=>$value)
		{
			$time = str_replace($key, $value, $time);
		}
		return $time;
	}
}
?>
